package com.example.hao.qq.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

    public static  String get(String address){

        HttpURLConnection huc = null;
        BufferedReader br = null;
        try {
            URL url = new URL(address);

            huc = (HttpURLConnection) url.openConnection();

            huc.setConnectTimeout(5000);
            huc.setRequestMethod("GET");
            int responseCode = huc.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK){
                System.out.println("连接成功");
                InputStream is = huc.getInputStream();
                br = new BufferedReader(new InputStreamReader(is));

                String s = br.readLine();

                return s;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (huc != null){
                huc.disconnect();
            }
        }
        return null;
    }

}
